package com.szxw.BaiduAi.utils;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;

import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;

/**
 * @Author: 苏执小威
 * @Email:1410511702@qq.com
 * @Desc: 主要完成HttpServletRequest中读取文件的功能
 * @Date: 2022/9/22 14:26
 */
public class FileUtils {

    protected static Logger logger = LoggerFactory.getLogger(FileUtils.class);
    /**
     * 该方法用于在HttpServletRequest对象中获取数据并返回
     * @param req
     */
    public static byte[] getFileDataByRequest(HttpServletRequest req){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream inputStream = null;
        try {
            DiskFileItemFactory disk = new DiskFileItemFactory();
            ServletFileUpload fileUpload = new ServletFileUpload(disk);
            fileUpload.setHeaderEncoding("utf-8");
            ServletRequestContext context = new ServletRequestContext( req);
            List<FileItem> fileItems = fileUpload.parseRequest(context);
            for (FileItem fileItem : fileItems) {
                    if(!fileItem.isFormField()){
                        inputStream = fileItem.getInputStream();
                        byte[] buff = new byte[1024];
                        int len = 0;
                        while((len = inputStream.read(buff)) != -1){
                            out.write(buff,0,len);
                        }

                    }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

                try {
                    if(inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        return out.toByteArray();
    }


    public static byte[] fileToBytes(String filePath) {
        byte[] buffer = null;
        File file = new File(filePath);
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            buffer = bos.toByteArray();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            logger.error("", ex);
        } catch (IOException ex) {
            ex.printStackTrace();
            logger.error("", ex);
        } finally {
            try {
                if (null != bos) {
                    bos.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
                logger.error("", ex);
            } finally {
                try {
                    if (null != fis) {
                        fis.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                    logger.error("", ex);
                }
            }
        }
        return buffer;
    }
}
